import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue"
Vue.use(VueRouter);
const routes = [{
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    redirect: "/home/all",
    children: [{
        path: "all",
        name: "All",
        component: () => import("../components/All")
      },
      {
        path: "hao",
        name: "Hao",
        component: () => import("../components/Hao")
      },
      {
        path: "zhu",
        name: "Zhu",
        component: () => import("../components/Zhu")
      },
      {
        path: "userlist",
        name: "Userlist",
        component: () => import("../components/Userlist")
      },
    ]
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/about",
    name: "About",
    component: () => import("../views/About.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => { //路由守卫里面不能使用循环
  console.log(to, from, )
  // 权限认证
  let toKen = localStorage.getItem("toKen")
  console.log(toKen)
  if (to.path === '/login') {
    next()
  } else {
    // 非登录页，需要权限
    if (toKen) { //有权限直接下去
      next()
    } else { //没有权限去登陆获得权限
      next({
        name: "Login"
      })
    }
  }

})
export default router;